<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/15
 * Time: 15:03
 */
namespace app\index\controller;

use app\common\lib\Redis;
use app\common\lib\redis\Predis;
use app\common\lib\Util;

class Login
{
    public function index()
    {
        $phone_num = intval($_POST['phone_num']);
        $code = intval($_POST['code']);

        if(empty($phone_num) || empty($code)){
            return Util::show(config('code.error'), 'phone or code is error');
        }
        $redis_code = Predis::getInstance()->get(Redis::smsKey($phone_num));
        if($redis_code == $code){
            //验证之后，把redis里面的验证码清空
            Predis::getInstance()->del(Redis::smsKey($phone_num));

            $data = [
                'user' => $phone_num,
                'srcKey' => md5(Redis::userKey($phone_num)),
                'time' => time(),
                'isLogin' => true,
            ];
            Predis::getInstance()->set(Redis::userKey($phone_num), $data);

            return Util::show(config('code.success'), 'ok', $data);
        }else{
            return Util::show(config('code.error'), 'login error');
        }
    }
}